package com.bawei.image

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.loader.FrescoImageLoader
import com.bawei.image.loader.GlideImageLoader
import com.bawei.image.loader.NativeImageLoader
import com.bawei.image.loader.PicassoImageLoader
import java.io.File

/**
 * 作者：Jason
 * 日期：2023/10/18
 * 描述：图片策略加载器管理对象
 */
class ImgLoader private constructor(
    private var strategy: ImageStrategy
) : IImageLoader {

    private var imgLoader = when (strategy) {
        ImageStrategy.GLIDE -> GlideImageLoader()
        ImageStrategy.FRESCO -> FrescoImageLoader()
        ImageStrategy.PICASSO -> PicassoImageLoader()
        ImageStrategy.NATIVE -> NativeImageLoader()
    }

    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoader.loadFileIntoImageView(context, file, target, options)
    }

    override fun loadResourceIntoImageView(
        context: Context,
        resource: Int,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoader.loadResourceIntoImageView(context, resource, target, options)
    }

    override fun loadUriIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoader.loadUriIntoImageView(context, uri, target, options)
    }

    override fun loadByteArrayIntoImageView(
        context: Context,
        bytes: ByteArray,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoader.loadByteArrayIntoImageView(context, bytes, target, options)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoader.loadUrlIntoImageView(context, url, target, options)
    }

    companion object Builder {
        private lateinit var strategy: ImageStrategy
        fun setImageStrategy(strategy: ImageStrategy):Builder{
            this.strategy = strategy
            return this
        }
        fun build(): ImgLoader = ImgLoader(strategy)
    }


}